home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / gfx / show / gs_src_gs.lha / gs5.03 / gdevccr.c < prev    next >
C/C++ Source or Header  |  1996-05-15  |  8KB  |  295 lines

  1. /* Copyright (C) 1996 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevccr.c */
  20. /* CalComp Raster Format driver */
  21. #include "gdevprn.h"
  22.  
  23. /*
  24.  * Please contact the author, Ernst Muellner (ernst.muellner@oenzl.siemens.de),
  25.  * if you have any questions about this driver.
  26.  */
  27.  
  28. #define CCFILESTART(p) putc(0x02, p)
  29. #define CCFILEEND(p) putc(0x04, p)
  30. #define CCNEWPASS(p) putc(0x0c, p)
  31. #define CCEMPTYLINE(p) putc(0x0a, p)
  32. #define CCLINESTART(len,p) do{ putc(0x1b,p);putc(0x4b,p);putc(len>>8,p); \
  33.                    putc(len&0xff,p);} while(0)
  34.  
  35. #define CPASS (0)
  36. #define MPASS (1)
  37. #define YPASS (2)
  38. #define NPASS (3)
  39.  
  40.  
  41. typedef struct cmyrow_s
  42.       {
  43.         int current;
  44.             int _cmylen[NPASS];
  45.         int is_used;
  46.         char cname[4];
  47.         char mname[4];
  48.         char yname[4];
  49.             unsigned char *_cmybuf[NPASS];
  50.       } cmyrow;
  51.  
  52. #define clen _cmylen[CPASS]
  53. #define mlen _cmylen[MPASS]
  54. #define ylen _cmylen[YPASS]
  55. #define cmylen _cmylen
  56.  
  57. #define cbuf _cmybuf[CPASS]
  58. #define mbuf _cmybuf[MPASS]
  59. #define ybuf _cmybuf[YPASS]
  60. #define cmybuf _cmybuf
  61.  
  62. private int alloc_rb( cmyrow **rb, int rows);
  63. private int alloc_line( cmyrow *row, int cols);
  64. private void add_cmy8(cmyrow *rb, char c, char m, char y);
  65. private void write_cpass(cmyrow *buf, int rows, int pass, FILE * pstream);
  66. private void free_rb_line( cmyrow *rbuf, int rows, int cols);
  67.  
  68. struct gx_device_ccr_s {
  69.     gx_device_common;
  70.     gx_prn_device_common;
  71.         /* optional parameters */
  72. };
  73. typedef struct gx_device_ccr_s gx_device_ccr;
  74.  
  75. #define bdev ((gx_device_ccr *)pdev)
  76.  
  77. /* ------ The device descriptors ------ */
  78.  
  79. /*
  80.  * Default X and Y resolution.
  81.  */
  82. #define X_DPI 300
  83. #define Y_DPI 300
  84. #define DEFAULT_WIDTH_10THS_A3 117
  85. #define DEFAULT_HEIGHT_10THS_A3 165
  86.  
  87. /* Macro for generating ccr device descriptors. */
  88. #define ccr_prn_device(procs, dev_name, margin, num_comp, depth, max_gray, max_rgb, print_page)\
  89. {    prn_device_body(gx_device_ccr, procs, dev_name,\
  90.       DEFAULT_WIDTH_10THS_A3, DEFAULT_HEIGHT_10THS_A3, X_DPI, Y_DPI,\
  91.       margin, margin, margin, margin,\
  92.       num_comp, depth, max_gray, max_rgb, max_gray + 1, max_rgb + 1,\
  93.       print_page)\
  94. }
  95.  
  96. /* For CCR, we need our own color mapping procedures. */
  97. private dev_proc_map_rgb_color(ccr_map_rgb_color);
  98. private dev_proc_map_color_rgb(ccr_map_color_rgb);
  99.  
  100.  
  101. /* And of course we need our own print-page routine. */
  102. private dev_proc_print_page(ccr_print_page);
  103.  
  104. /* The device procedures */
  105. private gx_device_procs ccr_procs =
  106.     prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  107.             ccr_map_rgb_color, ccr_map_color_rgb);
  108.  
  109. /* The device descriptors themselves */
  110. gx_device_ccr far_data gs_ccr_device =
  111.   ccr_prn_device(ccr_procs, "ccr", 0.2, 3, 8, 1, 1,
  112.          ccr_print_page);
  113.  
  114. /* ------ Color mapping routines ------ */
  115. /* map an rgb color to a ccr cmy bitmap */
  116. private gx_color_index
  117. ccr_map_rgb_color(gx_device *pdev, ushort r, ushort g, ushort b)
  118. {
  119.   register int shift = gx_color_value_bits - 1;
  120.   r>>=shift;
  121.   g>>=shift;
  122.   b>>=shift;
  123.  
  124.   r=1-r; g=1-g; b=1-b; /* rgb -> cmy */
  125.   return r<<2 | g<<1 | b;
  126. }
  127.  
  128. /* map an ccr cmy bitmap to a rgb color */ 
  129. private int
  130. ccr_map_color_rgb(gx_device *pdev, gx_color_index color, ushort rgb[3])
  131. {
  132.   rgb[2]=(1-(color >>2))*gx_max_color_value; /* r */
  133.   rgb[1]=(1-( (color & 0x2) >> 1))*gx_max_color_value; /* g */
  134.   rgb[0]=(1-(color & 0x1))*gx_max_color_value; /* b */
  135.   return 0;
  136. }
  137. /* ------ print page routine ------ */
  138.  
  139.  
  140. private int
  141. ccr_print_page(gx_device_printer *pdev, FILE *pstream)
  142. {
  143.   cmyrow *linebuf;
  144.   int line_size = gdev_prn_raster((gx_device *)pdev);
  145.   int pixnum = pdev->width;
  146.   int lnum = pdev->height;
  147.   int l, p, b;
  148.   int cmy, c, m, y;
  149.   byte *in;
  150.   byte *data;
  151.  
  152.   if((in = (byte *)gs_malloc(line_size, 1, "gsline")) == NULL)
  153.      return_error(gs_error_VMerror);
  154.     
  155.   if(alloc_rb( &linebuf, lnum))
  156.     {
  157.       gs_free(in, line_size, 1, "gsline");
  158.       return_error(gs_error_VMerror);
  159.     }
  160.  
  161.   for ( l = 0; l < lnum; l++ )
  162.      {    gdev_prn_get_bits(pdev, l, in, &data);
  163.         if(alloc_line(&linebuf[l], pixnum))
  164.       {
  165.         gs_free(in, line_size, 1, "gsline");
  166.         free_rb_line( linebuf, lnum, pixnum );
  167.         return_error(gs_error_VMerror);
  168.       }
  169.         for ( p=0; p< pixnum; p+=8)
  170.       {
  171.         c=m=y=0;
  172.             for(b=0; b<8; b++)
  173.         {
  174.               c <<= 1; m <<= 1; y <<= 1;
  175.           if(p+b < pixnum)
  176.         cmy = *data;
  177.           else
  178.         cmy = 0;
  179.  
  180.               c |= cmy>>2;
  181.           m |= (cmy>>1) & 0x1;
  182.           y |= cmy & 0x1;
  183.           data++;
  184.         }
  185.         add_cmy8(&linebuf[l], c, m, y);
  186.       }
  187.       }
  188. CCFILESTART(pstream);
  189. write_cpass(linebuf, lnum, YPASS, pstream);
  190. CCNEWPASS(pstream);
  191. write_cpass(linebuf, lnum, MPASS, pstream);
  192. CCNEWPASS(pstream);
  193. write_cpass(linebuf, lnum, CPASS, pstream);
  194. CCFILEEND(pstream);         
  195.  
  196. /* clean up */          
  197. gs_free(in, line_size, 1, "gsline");
  198. free_rb_line( linebuf, lnum, pixnum );
  199. return 0;
  200. }
  201.  
  202.  
  203. /* ------ Internal routines ------ */
  204.  
  205.  
  206. private int alloc_rb( cmyrow **rb, int rows)
  207.   {
  208.   *rb = (cmyrow*) gs_malloc(rows, sizeof(cmyrow), "rb");
  209.   if( *rb == 0)
  210.     return_error(gs_error_VMerror);
  211.   else
  212.     {
  213.       int r;
  214.       for(r=0; r<rows; r++)
  215.     {
  216.       sprintf((*rb)[r].cname, "C%02x", r);
  217.       sprintf((*rb)[r].mname, "M%02x", r);
  218.       sprintf((*rb)[r].yname, "Y%02x", r);
  219.       (*rb)[r].is_used=0;
  220.     }
  221.       return 0;
  222.     }
  223. }
  224.  
  225. private int alloc_line( cmyrow *row, int cols)
  226. {
  227.   int suc;
  228.   suc=((row->cbuf = (char *) gs_malloc(cols,1, row->cname)) &&
  229.        (row->mbuf = (char *) gs_malloc(cols,1, row->mname)) &&
  230.        (row->ybuf = (char *) gs_malloc(cols,1, row->yname)));
  231.   if(suc == 0)
  232.        {
  233.        gs_free(row->cbuf, cols,1, row->cname);
  234.        gs_free(row->mbuf, cols,1, row->mname);
  235.        gs_free(row->ybuf, cols,1, row->yname);
  236.  
  237.        return_error(gs_error_VMerror);
  238.      }
  239.   row->is_used = 1;
  240.   row->current = row->clen = row->mlen = row->ylen = 0;
  241.   return 0;
  242. }
  243.  
  244. private void add_cmy8(cmyrow *rb, char c, char m, char y)
  245. {
  246.   int cur=rb->current;
  247.   rb->cbuf[cur]=c;
  248.   if(c)
  249.     rb->clen=cur+1;
  250.   rb->mbuf[cur]=m;
  251.   if(m)
  252.     rb->mlen=cur+1;
  253.   rb->ybuf[cur]=y;
  254.   if(y)
  255.     rb->ylen=cur+1;
  256.   rb->current++;
  257.   return;
  258. }
  259.  
  260. private void write_cpass(cmyrow *buf, int rows, int pass, FILE * pstream)
  261. {
  262.   int row, len;
  263.     for(row=0; row<rows; row++)
  264.       {
  265.       len=buf[row].cmylen[pass];
  266.       if(len == 0)
  267.     CCEMPTYLINE(pstream);
  268.       else
  269.     {
  270.       CCLINESTART(len,pstream);
  271.           fwrite( buf[row].cmybuf[pass], len, 1, pstream);
  272.         }
  273.     }
  274.   return;
  275. }
  276.  
  277. private void free_rb_line( cmyrow *rbuf, int rows, int cols)
  278. {
  279.   int i;
  280.   for(i=0; i<rows; i++)
  281.     {
  282.       if(rbuf[i].is_used)
  283.     {
  284.       gs_free(rbuf[i].cbuf, cols, 1, rbuf[i].cname);
  285.       gs_free(rbuf[i].mbuf, cols, 1, rbuf[i].mname);
  286.       gs_free(rbuf[i].ybuf, cols, 1, rbuf[i].yname);
  287.       rbuf[i].is_used = 0;
  288.     }
  289.       else
  290.     break;
  291.     }
  292.   gs_free( rbuf, rows, sizeof(cmyrow),  "rb");
  293.   return;
  294. }
  295.